package com.wholesale;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.wholesale.beans.OrderTrackingInfo;
import com.wholesale.datas.WebServiceClient;
import com.wholesale.utils.Constants;
import com.wholesale.utils.TrackingPackagesListAdapter;

public class MyOrderTrackingsLayout extends LinearLayout implements
		OnClickListener {
	private ViewFlipper viewFlipper;
	private Context context;
	private View baseView;
	private ListView bodyList;
	private Button leftButton;
	private Button rightButton;
	private TextView headView;
	private TrackingPackagesListAdapter listAdapter;
	private MyTrackListAsync mytrac;
	private ArrayList<OrderTrackingInfo> orderTrack;
	private RelativeLayout loadingLayout;

	// 构造函数
	public MyOrderTrackingsLayout(Context context, final ViewFlipper viewFlipper) {
		super(context);
		this.viewFlipper = viewFlipper;
		this.context = context;
		// 加载需要的属性，加载方法一的子Layout
		baseView = ((Activity) getContext()).getLayoutInflater().inflate(
				R.layout.common, this);
		bodyList = (ListView) baseView.findViewById(R.id.bodyList);
		leftButton = (Button) baseView.findViewById(R.id.leftBtn);
		leftButton.setText("My account");
		leftButton.setOnClickListener(this);
		rightButton = (Button) baseView.findViewById(R.id.rightBtn);
		rightButton.setVisibility(View.INVISIBLE);
		headView = (TextView) baseView.findViewById(R.id.title);
		headView.setText("Packages");
		loadingLayout = (RelativeLayout) baseView
				.findViewById(R.id.loadingLayout);

		bodyList.setSelector(R.drawable.comments_list_bg);
		// item 单击事件
		bodyList.setOnItemClickListener(moreClieckListener);
		listAdapter = new TrackingPackagesListAdapter(context, bodyList);
		bodyList.setAdapter(listAdapter);
		mytrac = new MyTrackListAsync(context);
		mytrac.execute(false);
	}

	// ListView 点击事件
	OnItemClickListener moreClieckListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			OrderTrackingInfo info = orderTrack.get(arg2);
			if (info.getShippingStatus() == 1 || info.getShippingStatus() == 2) {
				TrackingUrlAsync trackingUrlAsync = new TrackingUrlAsync(
						context);
				trackingUrlAsync.execute(orderTrack.get(arg2));
			} else {
				ProgressDialog pdialog;
				pdialog = new ProgressDialog(context);
				pdialog.setTitle("Sorry");
				pdialog.setMessage("There are no logistics information can be found!");
				pdialog.setCancelable(true);
				pdialog.setButton("cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int i) {
								dialog.cancel();
							}
						});
				pdialog.show();
			}
			// int orderid = orderTrack.get(arg2).getOrderId();
			// MyTracDetailLayout orderDetailLayout = new MyTracDetailLayout(
			// context, viewFlipper, orderid);
			// viewFlipper.addView(orderDetailLayout);
			// viewFlipper.showNext();
		}
	};

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftBtn:
			viewFlipper.showPrevious();
			viewFlipper.removeView(this);
			break;
		default:
			break;
		}

	}

	class MyTrackListAsync extends
			AsyncTask<Boolean, Integer, ArrayList<OrderTrackingInfo>> {
		ProgressDialog pdialog;

		public MyTrackListAsync(Context context) {
			// pdialog = new ProgressDialog(context);
			// pdialog.setTitle("Loading...");
			// pdialog.setMessage("Please wait...");
			// pdialog.setCancelable(true);
			// pdialog.setButton("cancel", new DialogInterface.OnClickListener()
			// {
			// public void onClick(DialogInterface dialog, int i) {
			// dialog.cancel();
			// MyTrackListAsync.this.cancel(true);
			// }
			// });
			// pdialog.show();
		}

		@Override
		protected void onPreExecute() {
			loadingLayout.setVisibility(View.VISIBLE);
		}

		@Override
		protected ArrayList<OrderTrackingInfo> doInBackground(Boolean... params) {

			orderTrack = WebServiceClient.getMyOrderTrackings(
					Constants.loginResponse.getUserId(),
					Constants.loginResponse.getUserToken());
			if (orderTrack == null) {
				return new ArrayList<OrderTrackingInfo>();
			} else {
				return orderTrack;
			}
		}

		@Override
		protected void onPostExecute(ArrayList<OrderTrackingInfo> result) {
			listAdapter.clear();
			listAdapter.addItems(result);
			listAdapter.notifyDataSetChanged();
			// pdialog.dismiss();
			loadingLayout.setVisibility(View.GONE);
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}
	}

	class TrackingUrlAsync extends
			AsyncTask<OrderTrackingInfo, Integer, String> {
		ProgressDialog pdialog;

		public TrackingUrlAsync(Context context) {
			pdialog = new ProgressDialog(context);
			pdialog.setTitle("Sorry");
			pdialog.setMessage("There are no logistics information can ben found!");
			pdialog.setCancelable(true);
			pdialog.setButton("cancel", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int i) {
					dialog.cancel();
					TrackingUrlAsync.this.cancel(true);
				}
			});
		}

		@Override
		protected void onPreExecute() {
			loadingLayout.setVisibility(View.VISIBLE);
		}

		@Override
		protected String doInBackground(OrderTrackingInfo... params) {
			return WebServiceClient.getShippingTrackingUrl(params[0]);
		}

		@Override
		protected void onPostExecute(String result) {
			loadingLayout.setVisibility(View.GONE);
			if (result != null && !result.equals("")) {
				WebViewLayout webViewLayout = new WebViewLayout(context,
						viewFlipper, "Packages", "", result);
				viewFlipper.addView(webViewLayout);
				viewFlipper.showNext();
			} else {
				pdialog.show();
			}
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}
	}

}
